<template>
  <div v-loading="formLoading" class="formBox height-100 p-rem-0">
    <el-form ref="form" class="formTable p-rem-l-24  p-rem-r-24  p-rem-t-24 p-rem-b-80" :model="form" :rules="viewJumpType == 'form'?rules:{}" label-width="13rem">
      <el-row class="formRow">
        <el-col :span="12">
          <el-form-item label="新闻栏目" prop="newsType">
            <Treeselect v-model.trim="form.newsType"
                        :options="parentOptions"
                        :normalizer="normalizer"
                        :show-count="true"
                        placeholder="选择栏目"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="信息来源" prop="source">
            <el-input v-if="viewJumpType == 'form'" v-model.trim="form.source" placeholder="请输入" show-word-limit maxlength="25"></el-input>
            <span v-else>{{ form.title }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="新闻标题" prop="title">
            <el-input v-if="viewJumpType == 'form'" v-model.trim="form.title" placeholder="请输入" show-word-limit maxlength="120"></el-input>
            <span v-else>{{ form.title }}</span>
          </el-form-item>
        </el-col>
<!--        <el-col :span="12">-->
<!--          <el-form-item label="是否置顶" prop="isTop">-->
<!--            <el-switch v-if="viewJumpType == 'form'" v-model="form.isTop" inactive-value="N" active-value="Y" />-->
<!--            <span v-else>{{ getDictLabelMixins('sysYNOptions',form.isTop) }}</span>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :span="12">
          <el-form-item label="是否轮播" prop="isCarousel" placeholder="请选择">
            <el-switch v-if="viewJumpType == 'form'" v-model="form.isCarousel" inactive-value="N" active-value="Y" />
            <span v-else>{{ getDictLabelMixins('sysYNOptions',form.isCarousel) }}</span>
<!--            <el-select v-if="viewJumpType == 'form'" v-model.trim="form.isCarousel">-->
<!--              <el-option-->
<!--                v-for="item in sysYNOptions"-->
<!--                :key="item.dictValue"-->
<!--                :label="item.dictLabel"-->
<!--                :value="item.dictValue"/>-->
<!--            </el-select>-->
<!--            <span v-else>{{ getDictLabelMixins('sysYNOptions',form.isCarousel) }}</span>-->
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.isCarousel == 'Y'">
          <el-form-item label="图片" prop="licenceAnnexIds">
            <!-- uploadType  上传类型 image 图片 、 btnList 按钮 非表格附件list 、 btnTableList 按钮 表格附件list -->
            <uploadFile
              ref="uploadFile" uploaderName="licenceAnnex"
              uploadType="image" :uploadBtn="viewJumpType == 'form'"
              btnTitle="上传图片" btnType="primary"
              :isAccept="true" :acceptArr="['.jpg','.png','.jpeg']"
              :multiple="true" :limit="1" :showList="true"
              :deleteBool="viewJumpType == 'form'"
              :successList="form.licenceAnnexList"
              @deleteFileList="(data,deleteAnnexIdsBool)=>deleteFileTablelistMixins(data,deleteAnnexIdsBool,false,'form','','','licenceAnnex')"
              @getFileList="(data)=>getFileTableListMixins(data,false,'form','','','licenceAnnex',true, '')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="富文本编辑器" class="width-100" prop="content">
            <wangEitor
                ref="wangEitor"
                :readOnlyBool="viewJumpType == 'form' ? false : true"
                :class="viewJumpType == 'form' ? '' : 'form-wang-eitor'"
                class="width-100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件" prop="annexAnnexIds">
            <!-- uploadType  上传类型 image 图片 、 btnList 按钮 非表格附件list 、 btnTableList 按钮 表格附件list -->
            <uploadFile
              ref="uploadFile" uploaderName="annexAnnex"
              uploadType="btnList" :uploadBtn="viewJumpType == 'form'"
              btnTitle="上传附件" btnType="primary"
              :isAccept="true" :acceptArr="['.jpg','.png','.jpeg']"
              :multiple="true" :limit="1" :showList="true"
              :deleteBool="viewJumpType == 'form'"
              :successList="form.annexAnnexList"
              @deleteFileList="(data,deleteAnnexIdsBool)=>deleteFileTablelistMixins(data,deleteAnnexIdsBool,false,'form','','','annexAnnex')"
              @getFileList="(data)=>getFileTableListMixins(data,false,'form','','','annexAnnex',true, '')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="formBtn m-rem-b-0">
      <el-button v-if="viewJumpType == 'form'" type="primary" size="small" @click="submitForm('00')">保存</el-button>
      <el-button v-if="viewJumpType == 'form'" type="primary" size="small" @click="submitForm('01')">发布</el-button>
      <el-button size="small" @click="resetFormMixins">取消</el-button>
    </div>
  </div>
</template>
<script>
// 混入
import { formMixins } from '@/mixins/publicMixins/formMixins'
// 组件
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
// ------------------------------------------------------------------------
export default {
  name: 'FormView',
  mixins: [formMixins],
  components: { Treeselect },
  inject: ['viewJumpType'],
  provide() {
    return {
      provideAll: this
    }
  },
  data() {
    return {
      // 上级栏目
      parentOptions: [],
      // ID
      id: this.$route_query.id,
      // form 值
      form: {

        newsType: null,
        title: null,
        isCarousel: '0',
        isTop: '0',
        content: null,
        source: null,
        deleteAnnexIds: [],
        licenceAnnexList: [],
        licenceAnnexIds: [],
        annexAnnexList: [],
        annexAnnexIds: []
      },
      // form 验证
      rules: {
        newsType: [
          { required: true, message: '请选择栏目', trigger: 'change' }
        ],
        title: [
          { required: true, message: '请输入新闻标题', trigger: 'blur' }
        ],
        source: [
          { required: true, message: '请输入信息来源', trigger: 'blur' }
        ],
        licenceAnnexIds: [
          { required: true, message: '请上传图片', trigger: 'blur' }
        ],
        annexAnnexIds: [
          { required: false, message: '请上传附件', trigger: 'blur' }
        ],
        content: [{ required: true, msg: '富文本编辑器', validator: (rule, value, callback) => this.wangEitorFun(rule, value, callback, 'wangEitor'), trigger: 'blur' }]
      }
    }
  },

  mounted() {
    // 上级栏目
    this.getTreeSelectMixins('parentOptions', '/ent-news-type/getAll', {}, 'id')
    // 系统是否 Y/N
    this.getDictMixins('sysYNOptions', 'sys_yes_no')
    if (this.id) {
      this.detailApi()
    }
  },
  methods: {
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },

    /**
    * @description: 保存
    * @Author: baoxuhui
    * @return {*}
    */
    submitForm(status) {
      this.form.releaseStatus = status
      this.form.isTop = Number(this.form.isTop)
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let url = null; let type = null
          if (this.$route_query.id) {
            url = '/ent-news/update'
            type = '修改'
          } else {
            url = '/ent-news/save'
            type = '新增'
          }
          this.saveOrUpdateMixins(url, this.form, type)
        }
      })
    },

    /**
     * @description: 调取详情
     * @Author: baoxuhui
     * @return {*}
     */
    detailApi() {
      const data = {
        id: this.id
      }
      const url = '/ent-news/get'
      this.getDetailMixins(url, data)
    },
    /**
     * @description: 详情回显
     * @Author: baoxuhui
     * @param {*} res 数据obj
     * @return {*}
     */
    formDetail(res) {
      const data = res.data
      // 回填富文本
      this.$refs.wangEitor.html = res.data.content
      this.form = data
      this.formLoading = false
    }
  }
}
</script>
